# Dickstein, Ho, and Mark (2023)
# This script creates data used to compare MLR payments to premiums, and medical markups

# Preliminaries
setwd("../../../../library")
source("PreliminariesCode.R")
library(gt)

data_location <- paste0(project_folder, "/data/orig")

build_location <- paste0(project_folder, "/analysis/tablesandfigures/build/adj_markups")
dir.create(build_location, recursive = T)

release_location <- paste0(project_folder, "/analysis/tablesandfigures/release/adj_markups")
dir.create(release_location, recursive = T)

# Loading data #
# Reinsurance, Risk-adjustment, and Risk Corridor payments
rira_dat <- fread(paste0(data_location, "/Risk Adjust, Reins, Risk Corridors/reins_ra_or.csv"))
rira_dat$in_rira <- 1

rc_dat <- fread(paste0(data_location, "/Risk Adjust, Reins, Risk Corridors/risk_corridors_or.csv"))
rc_dat$in_rc <- 1

# MLR data
mlr_dat <- fread(paste0(data_location, "/Risk Adjust, Reins, Risk Corridors/MLR data for comparison/insurerlevelstats.csv"))[total_lives > 1000]
mlr_dat$in_mlr <- 1

# Merging them together: 
combined_dat <- merge(
  mlr_dat, 
  rira_dat,
  by.x = c("HIOS_ISSUER_ID", "year"), 
  by.y= c("HIOS ID", "Year"), 
  all = T
)

combined_dat <- merge(
  combined_dat, 
  rc_dat,
  by.x = c("HIOS_ISSUER_ID", "year"), 
  by.y= c("HIOS ID", "Year"), 
  all = T
)

# Kondo!
combined_dat <- combined_dat[, .(
  hios = HIOS_ISSUER_ID, 
  name = ifelse(is.na(`HIOS INPUTTED INSURANCE COMPANY NAME.x`), `HIOS INPUTTED INSURANCE COMPANY NAME.y`, `HIOS INPUTTED INSURANCE COMPANY NAME.x`), 
  market = MARKET, 
  year,
  total_prem, 
  total_cost, 
  total_lives, 
  medical_markup, 
  reinsurance = ifelse(MARKET == "INDIVIDUAL", as.numeric(`REINSURANCE PAYMENT AMOUNT (OR NOT ELIGIBLE)`), 0),
  riskcorridor = ifelse(
    MARKET == "INDIVIDUAL", 
    as.numeric(`HHS RISK CORRIDOR AMOUNT (INDIVIDUAL MARKET)`), 
    as.numeric(`HHS RISK CORRIDOR AMOUNT (SMALL GROUP MARKET)`)),
  riskadjustment = ifelse(
    MARKET == "INDIVIDUAL", 
    as.numeric(`HHS RISK ADJUSTMENT TRANSFER AMOUNT (INDIVIDUAL MARKET, INCLUDING CATASTROPHIC) `), 
    as.numeric(`HHS RISK ADJUSTMENT TRANSFERS AMOUNT (SMALL GROUP MARKET)`)), 
  prorated_amount = ifelse(
    year == 2014, 
    ifelse(MARKET == "INDIVIDUAL", 
      as.numeric(`PRORATED AMOUNT (INDIVIDUAL MARKET)`), 
      as.numeric(`PRORATED AMOUNT (SMALL GROUP MARKET)`)), 
        ifelse(MARKET == "INDIVIDUAL", 
        as.numeric(`HHS RISK CORRIDOR AMOUNT (INDIVIDUAL MARKET)`), 
        as.numeric(`HHS RISK CORRIDOR AMOUNT (SMALL GROUP MARKET)`))),
  e_payments_to_2014 = as.numeric(`EXPECTED PAYMENT TOWARD 2014 AMOUNTS`), 
  in_mlr, 
  in_rira, 
  in_rc), ]

# Export data to add reinsurance, riskcorridor, riskadjustment to other future cost variables
fwrite(combined_dat, paste0(build_location, "/combined_dat.csv"))

combined_dat[, reinsurance_perlife := reinsurance / total_lives, ]
combined_dat[, riskcorridor_perlife := riskcorridor / total_lives, ]
combined_dat[, riskadjustment_perlife := riskadjustment / total_lives, ]
combined_dat[, gtransfers_perlife := (reinsurance + riskcorridor + riskadjustment) / total_lives, ]

# Keeping only values we know in mlr:
combined_dat <- combined_dat[in_mlr == 1]
ifnaz <- function(x) ifelse(is.na(x), 0, x)
combined_dat[, adjusted_medical_markup := (ifnaz(total_prem) + ifnaz(reinsurance) + ifnaz(riskcorridor) + ifnaz(riskadjustment))/ total_cost, ]
combined_dat[, market2 := fcase(
  market == "INDIVIDUAL", "Individual", 
  market == "SMALLGROUP", "Small Group"
)]

# Restricting to observations with more than 5000 lives: 
adj_data <- combined_dat[total_lives > 5000 & year %in% c(2014:2016)]

# Export data to create graphs in STATA
write.csv(adj_data, paste0(build_location, "/combined_dat_adj.csv"), row.names = FALSE)

